<template>
  <div class="users">




 <Row>
        <Col span="4"><Tree :data="treeData" @on-select-change="onTreeSelectChange"></Tree></Col>
        <Col span="20">

        <Button type="primary" @click="addData">添加数据</Button>
        <Button type="error" @click="removesData">删除多条</Button>
        <Input search v-model="name" placeholder="请输入关键字" @on-search="searchData"/>

        <Table :columns="columns" :data="listData" @on-selection-change="onSelectionChange"></Table>
        <Page :total="total" show-sizer @on-change="onChangePage" @on-page-size-change="onPageSizeChange" :page-size-opts="[5,10,20,30,40,50]"/>
        <Modal
        v-model    = "modal1"
        title      = "Common Modal dialog box title"
        @on-ok     = "ok"
        @on-cancel = "cancel">
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
             <FormItem label="新闻分类ID" prop="newsId">
                <Input v-model="formValidate.newsId"></Input>
            </FormItem>

            <FormItem label="新闻标题" prop="title">
                <Input v-model="formValidate.title" placeholder="Enter your title"></Input>
            </FormItem>

            <quill-editor v-model="formValidate.content"
                ref="myQuillEditor"
                >
            </quill-editor>
             <FormItem label="关键字" prop="keywords">
                <Input v-model="formValidate.keywords" placeholder="Enter keywords"></Input>
            </FormItem>
            <FormItem label="发布时间" prop="time">
                <DatePicker type="date" v-model="formValidate.time" placeholder="Select date"></DatePicker>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
                <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
            </FormItem>
        </Form>
    </Modal>

        </Col>

    </Row>


  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

import  { Base } from '../mixins/Base'
export default {
  components: {
    quillEditor
  },
  mixins:[Base],
  data(){
    return {
      treeData:[],
      module: 'news',
      formValidate: {
          newsId: null,
          title    : '',
          content: '',
          keywords:"",
          time:""
      },
      ruleValidate: {
          title: [
              { required: true, message: 'The title cannot be empty', trigger: 'blur' }
          ],
          content: [
              { required: true, message: 'content cannot be empty', trigger: 'blur' },
          ],
          keywords: [
              { required: true, message: 'keywords cannot be empty', trigger: 'blur' },
          ],
          time: [
               { required: true, type: 'date', message: 'Please select the date', trigger: 'change' },
          ],
      },
      columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
              title: '新闻标题',
              key  : 'title'
          },
          {
              title: '评论数',
              key  : 'commentCount'
          },
          {
              title: '新闻关键字',
              key  : 'keywords'
          },
          {
              title: '发布时间',
              key  : 'time',
               render: (h, params) => {
                    let time = this.$moment(params.row.time).format('YYYY-MM-DD');
                    return h('div', [
                        h('span', time)
                    ]);
                }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
                return h('div', [
                     h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginLeft: '-12px'
                        },
                        on: {
                            click: () => {
                                // this.show(params.index)
                                this.change()
                            }
                        }
                    }, '评论'),
                    h('Button', {
                        props: {
                            type: 'primary',
                            size: 'small'
                        },
                        style: {
                            marginRight: '2px',
                            marginLeft: '2px'
                        },
                        on: {
                            click: () => {
                                // this.show(params.index)
                                this.editData(params.row._id);
                            }
                        }
                    }, '修改'),
                    h('Button', {
                        props: {
                            type: 'error',
                            size: 'small'
                        },
                        on: {
                            click: () => {
                                this.removeData(params.row._id);
                            }
                        }
                    }, '删除')
                ]);
            }
        }
      ]
    }
  },
  methods:{
      onTreeSelectChange(node){
          console.log(node[0]._id)
          this.formValidate.newsId = node[0]._id;
          this.getData();
      },
      addData(){
          if(this.formValidate.newsId === null){
              alert('请选择一个分类');
          }else{
              this.modal1 = true;
          }
      },
      getTreeData(){
          this.axios({
              url:`http://localhost:3000/cate/list/1`,
              method:'post'
          }).then(res=>{
              console.log(res.data);
              this.treeData = res.data;
          })
      }
  },
  mounted(){
      this.getTreeData();
  }
}
</script>
